package com.czk.yygh.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czk.cmn.feignclient.DictFeignClient;
import com.czk.enums.DictEnum;
import com.czk.model.user.Patient;
import com.czk.yygh.user.mapper.PatientMapper;
import com.czk.yygh.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/25 20:47
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {
    @Autowired
    private DictFeignClient dictFeignClient;

    @Override
    public List<Patient> findAllByUserId(Long userId) {
        QueryWrapper<Patient> wrapper = new QueryWrapper();
        wrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        // 进一步封装
        // 远程调用获取数据字典里面的数据
        patientList.forEach(patient -> {
            this.packPatient(patient);
        });
        return patientList;
    }

    private void packPatient(Patient patient) {
        // 根据证件类型编码，获取证件具体类型
        String certificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());
        // 联系人证件类型
        String contactsCertificatesTypeString =
                dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        // 省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        // 市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        // 区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());
        // 封装内容
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
    }

    @Override
    public Patient getPatient(Long id) {
        Patient patient = baseMapper.selectById(id);
        this.packPatient(patient);
        return patient;
    }
}
